/*
 *      gconf_notebook.cpp
 *      
 *      Copyright 2010 AzureSky <AzureSky.fedora@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "gconf_notebook.h"

gconf_notebook::gconf_notebook(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade):
	Gtk::Notebook(cobject),
	refXml(refGlade)
{
	font_page();
	style_page();
	wallpager_page();
	lock_page();
}

gconf_notebook::~gconf_notebook()
{
}

void gconf_notebook::font_page()
{
	refXml -> get_widget("pro_font",pro_button);
	const std::string& font_name_temp(cur_gconf.get_string("/desktop/gnome/interface/font_name"));
	const Glib::ustring& font_name = Glib::locale_to_utf8(font_name_temp);
	pro_button -> set_font_name(font_name);
	pro_button -> signal_font_set().connect(sigc::bind<std::string,Gtk::FontButton*>( sigc::mem_fun(*this,&gconf_notebook::change_font),"/desktop/gnome/interface/font_name",pro_button));
	
	refXml -> get_widget("doc_font",doc_button);
	const std::string& doc_name_temp(cur_gconf.get_string("/desktop/gnome/interface/document_font_name"));
	const Glib::ustring& doc_name = Glib::locale_to_utf8(doc_name_temp);
	doc_button -> set_font_name(doc_name);
	doc_button -> signal_font_set().connect(sigc::bind<std::string,Gtk::FontButton*>( sigc::mem_fun(*this,&gconf_notebook::change_font),"/desktop/gnome/interface/document_font_name",doc_button));
	
	
	refXml -> get_widget("desk_font",desk_button);
	const std::string& desk_name_temp(cur_gconf.get_string("/apps/nautilus/preferences/desktop_font"));
	const Glib::ustring& desk_name = Glib::locale_to_utf8(desk_name_temp);
	desk_button -> set_font_name(desk_name);
	desk_button -> signal_font_set().connect(sigc::bind<std::string,Gtk::FontButton*>( sigc::mem_fun(*this,&gconf_notebook::change_font),"/apps/nautilus/preferences/desktop_font",desk_button));
	
	
	refXml -> get_widget("title_font",title_button);
	const std::string& title_name_temp(cur_gconf.get_string("/apps/metacity/general/titlebar_font"));
	const Glib::ustring& title_name = Glib::locale_to_utf8(title_name_temp);
	title_button -> set_font_name(title_name);
	title_button -> signal_font_set().connect(sigc::bind<std::string,Gtk::FontButton*>( sigc::mem_fun(*this,&gconf_notebook::change_font),"/apps/metacity/general/titlebar_font",title_button));
	
	
	refXml -> get_widget("dengkuan_font",dengkuan_button);
	const std::string& dk_name_temp(cur_gconf.get_string("/desktop/gnome/interface/monospace_font_name"));
	const Glib::ustring& dk_name = Glib::locale_to_utf8(dk_name_temp);
	dengkuan_button -> set_font_name(dk_name);
	dengkuan_button -> signal_font_set().connect(sigc::bind<std::string,Gtk::FontButton*>( sigc::mem_fun(*this,&gconf_notebook::change_font),"/desktop/gnome/interface/monospace_font_name",dengkuan_button));
	
	refXml -> get_widget("danshe",m_b1);
	refXml -> get_widget("duibi",m_b2);
	refXml -> get_widget("xingzhuang",m_b3);
	refXml -> get_widget("cixiangsu",m_b4);
	
	group = m_b1 -> get_group();
	//m_b1 -> set_active();
	m_b2 -> set_group(group);
	m_b3 -> set_group(group);
	m_b4 -> set_group(group);
	
	std::string antialiasing = cur_gconf.get_string("/desktop/gnome/font_rendering/antialiasing");
	if(antialiasing.compare("none") == 0)
		m_b1 -> set_active();
	else{
		if(antialiasing.compare("rgba") == 0){
			m_b4 -> set_active();
		}else{
			std::string hinting = cur_gconf.get_string("/desktop/gnome/font_rendering/hinting");
			if(hinting.compare("full") == 0)
				m_b2 -> set_active();
			else
				m_b3 -> set_active();
		}
	}
	
	m_b1 -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::change_rending),1));
	m_b2 -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::change_rending),2));
	m_b3 -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::change_rending),3));
	m_b4 -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::change_rending),4));
	
	refXml -> get_widget("spin_dpi",dpi_spin);
	dpi_spin -> set_value(cur_gconf.get_float("/desktop/gnome/font_rendering/dpi"));
	dpi_spin -> signal_value_changed().connect(sigc::mem_fun(*this,&gconf_notebook::change_dpi));
}

void gconf_notebook::change_rending(int index)
{
	switch(index){
		case 1:{
			cur_gconf.set_string("/desktop/gnome/font_rendering/antialiasing","none");
			cur_gconf.set_string("/desktop/gnome/font_rendering/hinting","full");
			cur_gconf.set_string("/desktop/gnome/font_rendering/rgba_order","rgb");
		}break;
		case 2:{
			cur_gconf.set_string("/desktop/gnome/font_rendering/antialiasing","grayscale");
			cur_gconf.set_string("/desktop/gnome/font_rendering/hinting","full");
			cur_gconf.set_string("/desktop/gnome/font_rendering/rgba_order","rgb");
		}break;
		case 3:{
			cur_gconf.set_string("/desktop/gnome/font_rendering/antialiasing","grayscale");
			cur_gconf.set_string("/desktop/gnome/font_rendering/hinting","slight");
			cur_gconf.set_string("/desktop/gnome/font_rendering/rgba_order","rgb");
		}break;
		case 4:{
			cur_gconf.set_string("/desktop/gnome/font_rendering/antialiasing","rgba");
			cur_gconf.set_string("/desktop/gnome/font_rendering/hinting","full");
			cur_gconf.set_string("/desktop/gnome/font_rendering/rgba_order","rgb");
		}break;
	}
}

void gconf_notebook::change_dpi()
{
	cur_gconf.set_float("/desktop/gnome/font_rendering/dpi",dpi_spin -> get_value());
	dpi_spin -> set_value(cur_gconf.get_float("/desktop/gnome/font_rendering/dpi"));
}

void gconf_notebook::change_font(std::string path,Gtk::FontButton* button)
{
	cur_gconf.set_string(path.c_str(),button -> get_font_name());
}

void gconf_notebook::style_page()
{
	refXml -> get_widget("button_icon",button_icon);
	button_icon -> set_active(cur_gconf.get_bool("/desktop/gnome/interface/buttons_have_icons"));
	button_icon -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/interface/buttons_have_icons",button_icon));
	
	refXml -> get_widget("menus_icon",menus_icon);
	menus_icon -> set_active(cur_gconf.get_bool("/desktop/gnome/interface/menus_have_icons"));
	menus_icon -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/interface/menus_have_icons",menus_icon));
	
	refXml -> get_widget("menus_tearoff",menus_tearoff);
	menus_tearoff -> set_active(cur_gconf.get_bool("/desktop/gnome/interface/menus_have_tearoff"));
	menus_tearoff -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/interface/menus_have_tearoff",menus_tearoff));
	
	refXml -> get_widget("show_desktop",show_desktop);
	show_desktop -> set_active(cur_gconf.get_bool("/apps/nautilus/preferences/show_desktop"));
	show_desktop -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/apps/nautilus/preferences/show_desktop",show_desktop));
	
	
	refXml -> get_widget("toolbar_icon_small",size_small);
	size_small -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::toolbar_size_change),1));
	
	refXml -> get_widget("toolbar_icon_large",size_large);
	size_large -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::toolbar_size_change),2));
	
	size_group = size_small -> get_group();
	size_large -> set_group(size_group);
	
	std::string size_string = cur_gconf.get_string("/desktop/gnome/interface/toolbar_icons_size");
	if(size_string.compare("small-toolbar") == 0)
		size_small -> set_active();
	else
		size_large -> set_active();
	
	refXml -> get_widget("style_both",style_both);
	style_both -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::toolbar_style_change),1));
	
	refXml -> get_widget("style_both_hori",style_both_horiz);
	style_both_horiz -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::toolbar_style_change),2));
	
	refXml -> get_widget("style_icon",style_icons);
	style_icons -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::toolbar_style_change),3));
	
	refXml -> get_widget("style_text",style_text);
	style_text -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::toolbar_style_change),4));
	
	style_group = style_both -> get_group();
	style_both_horiz -> set_group(style_group);
	style_icons -> set_group(style_group);
	style_text -> set_group(style_group);
	
	std::string style_string = cur_gconf.get_string("/desktop/gnome/interface/toolbar_style");
	if(style_string.compare("both") == 0)
		style_both -> set_active();
	else
		if(style_string.compare("both_horiz") == 0)
			style_both_horiz -> set_active();
		else
			if(style_string.compare("icons") == 0)
				style_icons -> set_active();
			else
				style_text -> set_active();
}

void gconf_notebook::change_bool(std::string path,Gtk::CheckButton* button)
{
	cur_gconf.set_bool(path.c_str(),button -> get_active());
}

void gconf_notebook::toolbar_size_change(int index)
{
	switch(index){
		case 1:{
			cur_gconf.set_string("/desktop/gnome/interface/toolbar_icons_size","small-toolbar");
		};break;
		case 2:{
			cur_gconf.set_string("/desktop/gnome/interface/toolbar_icons_size","large-toolbar");
		};break;
	}
}

void gconf_notebook::toolbar_style_change(int index)
{
		switch(index){
		case 1:
			cur_gconf.set_string("/desktop/gnome/interface/toolbar_style","both");break;
		case 2:
			cur_gconf.set_string("/desktop/gnome/interface/toolbar_style","both_horiz");break;
		case 3:
			cur_gconf.set_string("/desktop/gnome/interface/toolbar_style","icons");break;
		case 4:
			cur_gconf.set_string("/desktop/gnome/interface/toolbar_style","text");break;
		}
}

void gconf_notebook::wallpager_page()
{
	refXml -> get_widget("wall_image",wallpager_image);
	
	static Glib::RefPtr<Gdk::Pixbuf> temp_wall = Gdk::Pixbuf::create_from_file(cur_gconf.get_string("/desktop/gnome/background/picture_filename"));
	wall_picture = temp_wall -> scale_simple(240,150,Gdk::INTERP_BILINEAR);
	
	wallpager_image -> set(wall_picture);
	
	refXml -> get_widget("wallpager_file",wallpager_file_button);
	wallpager_filter.set_name("图片");
	wallpager_filter.add_pixbuf_formats();
	wallpager_file_button -> add_filter(wallpager_filter);
	wallpager_file_button -> signal_file_set().connect(sigc::mem_fun(*this,&gconf_notebook::load_wallpager));
	
	refXml -> get_widget("wallpager",wallpager_button);
	wallpager_button -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::wallpager_style),1));
	
	refXml -> get_widget("centered",centered_button);
	centered_button -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::wallpager_style),2));
	
	refXml -> get_widget("scaled",scaled_button);
	scaled_button -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::wallpager_style),3));
	
	refXml -> get_widget("stretched",stretched_button);
	stretched_button -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::wallpager_style),4));
	
	refXml -> get_widget("zoom",zoom_button);
	zoom_button -> signal_pressed().connect(sigc::bind<int>(sigc::mem_fun(*this,&gconf_notebook::wallpager_style),5));
		
	wall_group = wallpager_button -> get_group();
	centered_button -> set_group(wall_group);
	scaled_button -> set_group(wall_group);
	stretched_button -> set_group(wall_group);
	zoom_button -> set_group(wall_group);
	
	std::string wall_style_string = cur_gconf.get_string("/desktop/gnome/background/picture_options");
	if(wall_style_string.compare("wallpaper") == 0)
		wallpager_button -> set_active();
	else
		if(wall_style_string.compare("centered") == 0)
			centered_button -> set_active();
		else
			if(wall_style_string.compare("scaled") == 0)
				scaled_button -> set_active();
			else
				if(wall_style_string.compare("stretched") == 0)
					stretched_button -> set_active();
				else
					zoom_button -> set_active();
}

void gconf_notebook::wallpager_style(int index)
{
	switch(index){
		case 1:
			cur_gconf.set_string("/desktop/gnome/background/picture_options","wallpaper");break;
		case 2:
			cur_gconf.set_string("/desktop/gnome/background/picture_options","centered");break;
		case 3:
			cur_gconf.set_string("/desktop/gnome/background/picture_options","scaled");break;
		case 4:
			cur_gconf.set_string("/desktop/gnome/background/picture_options","stretched");break;
		case 5:
			cur_gconf.set_string("/desktop/gnome/background/picture_options","zoom");break;
	}
}

void gconf_notebook::load_wallpager()
{
	wall_temp_wall = Gdk::Pixbuf::create_from_file(wallpager_file_button -> get_filename());
	wall_picture = wall_temp_wall -> scale_simple(240,150,Gdk::INTERP_BILINEAR);
	
	wallpager_image -> set(wall_picture);
	cur_gconf.set_string("/desktop/gnome/background/picture_filename",wallpager_file_button -> get_filename());
}

void gconf_notebook::lock_page()
{
	refXml -> get_widget("disable_lock_screen",disable_lock_screen);
	disable_lock_screen -> set_active(cur_gconf.get_bool("/desktop/gnome/lockdown/disable_lock_screen"));
	disable_lock_screen -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/lockdown/disable_lock_screen",disable_lock_screen));
	
	refXml -> get_widget("disable_printing",disable_printing);
	disable_printing -> set_active(cur_gconf.get_bool("/desktop/gnome/lockdown/disable_printing"));
	disable_printing -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/lockdown/disable_printing",disable_printing));
	
	refXml -> get_widget("disable_print_setup",disable_print_setup);
	disable_print_setup -> set_active(cur_gconf.get_bool("/desktop/gnome/lockdown/disable_print_setup"));
	disable_print_setup -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/lockdown/disable_print_setup",disable_print_setup));
	
	refXml -> get_widget("disable_user_switching",disable_user_switching);
	disable_user_switching -> set_active(cur_gconf.get_bool("/desktop/gnome/lockdown/disable_user_switching"));
	disable_user_switching -> signal_clicked().connect(sigc::bind<std::string,Gtk::CheckButton*>(sigc::mem_fun(*this,&gconf_notebook::change_bool),"/desktop/gnome/lockdown/disable_user_switching",disable_user_switching));
	
}
